//package com.health.agent.server.controller;
//
//
//import com.health.agent.common.req.ChatRequest;
//import com.health.agent.server.service.AgentService;
//import com.health.common.domain.Department;
//import com.health.common.redis.service.RedisService;
//import com.health.income.remote.feign.RemoteIncomeService;
//
//import org.springframework.ai.chat.client.ChatClient;
//import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
//import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
//import org.springframework.ai.chat.memory.InMemoryChatMemory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.MediaType;
//import org.springframework.http.codec.ServerSentEvent;
//import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RestController;
//import reactor.core.publisher.Flux;
//
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//import java.util.stream.Collectors;
//
//
//@CrossOrigin("*")
//@RestController
//public class ChatbotController {
//
//    @Autowired
//    private AgentService agentService;
//    @Autowired
//    private RemoteIncomeService departmentService; // 注入Feign客户端
//    @Autowired
//    private RedisService redisService;
//
//    private  static String DCK  = "department_cache_key" ;
//    private final ChatClient chatClient;
//    private final InMemoryChatMemory inMemoryChatMemory;
//
//
//    public ChatbotController(ChatClient chatClient, InMemoryChatMemory inMemoryChatMemory) {
//        this.chatClient = chatClient;
//        this.inMemoryChatMemory = inMemoryChatMemory;
//    }
//
//
//    @PostMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public Flux<ServerSentEvent<String>> streamChat(@RequestBody ChatRequest request) {
//
//
//
//        String departmentList = getDepartmentList();
//
//        // 动态构建系统消息
//        String systemMessage = String.format("""
//        # 角色设定
//        你是医院智能分诊助手"康师傅"，专业严谨但语气亲切。需要结合上文和以下科室进行解答;
//
//        # 交互规则
//        ## 第一阶段：通过接收到患者首次描述后，进行分析并简单的询问{最多问3个问题}等待用户回答后再进行反问后进入第二阶段,
//        ## 第二阶段：分诊判断并添加祝福语
//        - 只有在第二阶段才可推荐科室
//        - 必须从以下科室中选择推荐，名称需完全匹配并用[]括起，例：建议去[神经内科]
//        """, departmentList);
//
//        return chatClient
//                .prompt()
//                .system(s -> s.text(systemMessage)) // 设置初始系统消息
//                .user(request.getMessage())  //输入用户输入信息
//                .advisors(new MessageChatMemoryAdvisor(inMemoryChatMemory, request.getUserId(), 10),new SimpleLoggerAdvisor())
//                .stream()
//                .content()
//                .map(content -> ServerSentEvent.builder(content).event("message").build())
//                .concatWithValues(ServerSentEvent.builder("[DONE]").build())
//                .onErrorResume(e -> Flux.just(ServerSentEvent.builder("Error: " + e.getMessage()).event("error").build()));
//
//    }
//
//
//
//
//    //获取科室列表从redis
//    private String getDepartmentList() {
//        //先判断redis中是否存在
//        if (!redisService.hasKey(DCK)) {
//            // 从远程服务获取科室列表
//            List<Department> departments = departmentService.getDepartment().getData();
//            redisService.setCacheObject(DCK, departments,1L,TimeUnit.DAYS);
//        }
//
//        List<Department> departments = redisService.getCacheObject(DCK);
//        //获取科室列表
//        String departmentList = departments.stream()
//                .map(Department::getDepartmentName)
//                .collect(Collectors.joining(", "));
//        return departmentList;
//    }
//
//
//
//}
